-
Notifications
You must be signed in to change notification settings - Fork 151
perf(particlesys): Optimize angleBetween() in Particle System #2218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
perf(particlesys): Optimize angleBetween() in Particle System #2218
Conversation
Greptile Overview
|
| Filename | Overview |
|---|---|
| Core/GameEngine/Source/GameClient/System/ParticleSys.cpp | Optimized angleBetween() by caching length calculations, eliminating 2 redundant sqrt operations per call |
Sequence Diagram
sequenceDiagram
participant Particle as Particle::updateParticle()
participant angleBetween as angleBetween()
participant Coord2D as Coord2D::length()
Note over Particle: m_particleUpTowardsEmitter == true
Particle->>Particle: Create upVec {0, 1}
Particle->>Particle: Create emitterDir from positions
Particle->>angleBetween: angleBetween(&upVec, &emitterDir)
Note over angleBetween: Before optimization:<br/>4 calls to length()
Note over angleBetween: After optimization:<br/>2 calls to length()
angleBetween->>Coord2D: vecA->length()
Coord2D-->>angleBetween: lengthA
angleBetween->>Coord2D: vecB->length()
Coord2D-->>angleBetween: lengthB
Note over angleBetween: Check if both lengths != 0
alt Both lengths are zero
angleBetween-->>Particle: return 0.0f
else Valid vectors
Note over angleBetween: Calculate dot product<br/>Calculate cosTheta<br/>Calculate theta using ACos
angleBetween-->>Particle: return ±theta
end
Particle->>Particle: Set m_angleZ = theta + PI
Mauller
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zoom zoom
| const Real lengthA = vecA->length(); | ||
| const Real lengthB = vecB->length(); | ||
|
|
||
| if (!(lengthA && lengthB)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe this should be !(vecA || vecB) before their use, or can they not be null at any point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I looked at call sites and they were not null, so I omitted null tests. I can relook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is only one caller and it never passes null, so this is safe.
This change optimizes the
angleBetween()function a bit by removing 2 redundantlengthfunction calls, which each containIt is used by the Particle System update.
Not measured, because impact is expected very minor.